from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from tqdm import tqdm
import csv
import time

# 设置Chrome选项
chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式，不打开浏览器界面
chrome_options.add_argument("--disable-gpu")

# 设置 Selenium WebDriver
driver = webdriver.Chrome(options=chrome_options)

# 设置显式等待
wait = WebDriverWait(driver, 15)

# 初始化 CSV 文件
output_csv = 'output.csv'
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)

    for i in tqdm(range(1, 5), desc="Processing"):
        # 打开网页
        url = "https://athena.ohdsi.org/search-terms/terms/" + str(i)
        driver.get(url)

        try:
            # 强制等待页面加载
            time.sleep(3)  # 适当延长加载时间以解决动态数据问题

            # 检查表格是否存在
            rows = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "li.ac-list-item")))

            # 初始化数据字典
            data_dict = {}

            for row in rows:
                try:
                    # 提取键和值
                    key = row.find_element(By.CLASS_NAME, 'at-term__attribute-name').text.strip()
                    list_elements = row.find_elements(By.TAG_NAME, 'span')
                    value = list_elements[-2].text.strip() if len(list_elements) >= 2 else ""
                    data_dict[key] = value
                except Exception as e:
                    print(f"跳过一行，错误: {e}")

            # 写入数据
            if i == 1:
                writer.writerow(data_dict.keys())  # 第一次写入标题行
            writer.writerow(data_dict.values())   # 写入数据行

        except Exception as e:
            print(f"页面加载失败或没有数据，错误: {e}")

        # 清理缓存
        driver.delete_all_cookies()
        driver.refresh()

# 关闭浏览器
driver.quit()